package com.example.myapplication.tack_30.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.myapplication.R;
import com.example.myapplication.tack_30.adapter.FenLeiAdapter;
import com.example.myapplication.tack_30.adapter.NearAdapter;
import com.example.myapplication.tack_30.adapter.RecommendAdapter;
import com.example.myapplication.tack_30.data.BannerData;
import com.example.myapplication.tack_30.data.FenLeiData;
import com.example.myapplication.tack_30.data.NearData;
import com.example.myapplication.tack_30.data.RecommendData;
import com.example.myapplication.utils.BannerTool;
import com.example.myapplication.utils.OkHttpTool;

import java.util.ArrayList;
import java.util.List;


public class HomekFragment extends Fragment {
    private ViewPager banner;
    private View view;
    private Handler handler;
    private RecyclerView fen_lei;
    private RecyclerView recommend;
    private RecyclerView near;
    private List<FenLeiData.DataDTO> listFenLei;
    private List<RecommendData.RowsDTO> listRecommendData;
    private List<NearData.RowsDTO> listNearData;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.tack_30_fragment_homek, container, false);
        init();
        getBanner();
        getFenLei();
        getRecommend();
        getNear();
        return view;

    }

    private void getNear() {
        String url = "prod-api/api/takeout/seller/near?pageNum=1&pageSize=8";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, NearData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                NearData nearData = (NearData)obj;
                if(nearData.getCode() == 200){
                    listNearData = nearData.getRows();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            near.setAdapter(new NearAdapter(listNearData,HomekFragment.this.getContext()));
                            near.setLayoutManager(new LinearLayoutManager(HomekFragment.this.getContext()));
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void getRecommend() {
        String url = "prod-api/api/takeout/seller/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, RecommendData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                RecommendData recommendData = (RecommendData)obj;
                if(recommendData.getCode() == 200){
                    listRecommendData = recommendData.getRows();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            recommend.setLayoutManager(new GridLayoutManager(HomekFragment.this.getContext(),2));
                            recommend.setAdapter(new RecommendAdapter(listRecommendData,HomekFragment.this.getContext()));
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void getFenLei() {
        String url = "prod-api/api/takeout/theme/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, FenLeiData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                FenLeiData fenLeiData = (FenLeiData)obj;
                if(fenLeiData.getCode() == 200){
                    listFenLei = fenLeiData.getData();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            fen_lei.setAdapter(new FenLeiAdapter(listFenLei,HomekFragment.this.getContext()));
                            fen_lei.setLayoutManager(new GridLayoutManager(HomekFragment.this.getContext(),5));
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void getBanner() {
        String url = "prod-api/api/takeout/rotation/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, BannerData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                BannerData bannerData = (BannerData)obj;

                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            if(bannerData.getCode() == 200) {
                                ArrayList<String> list = new ArrayList<>();
                                for (BannerData.RowsDTO row : bannerData.getRows()) {
                                    list.add(row.getAdvImg());
                                }
                                BannerTool bannerTool = new BannerTool(HomekFragment.this.getContext(), list, banner);
                                banner.setAdapter(bannerTool);
                            }
                        }
                    });

            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void init() {
        banner = view.findViewById(R.id.banner);
        fen_lei = view.findViewById(R.id.fen_lei);
        recommend = view.findViewById(R.id.recommend);
        near = view.findViewById(R.id.near);
        handler = new Handler();
    }
}